"""
@author: Sergiy Tsymbalyuk
"""

from sqlalchemy import Column, ForeignKey, Table
from sqlalchemy.types import Integer, String, Text, Boolean
from sqlalchemy.orm import relation, relationship

from pylons.i18n import lazy_ugettext as _

from chowa.model.meta import Base

__all__ = ['Panel', 'PanelBlock', 'panel_block_apps', 'HomepageBlock']


class Panel(Base):
    __tablename__ = 'layout_panel'
    
    id   = Column(Integer, primary_key=True)
    name = Column(String(30), info={'verbose': _('Object|Name')})
    blocks = relationship('PanelBlock', backref='panel')
    
    def __unicode__(self):
        return self.name


panel_block_apps = Table('layout_panel_block_app', Base.metadata,
    Column('panelblock_id', Integer, ForeignKey('layout_panel_block.id')),
    Column('app_id', Integer, ForeignKey('site_app.id'))
)
    

class PanelBlock(Base):
    __tablename__ = 'layout_panel_block'
    
    id = Column(Integer, primary_key=True)
    name = Column(String(30), info={'verbose': _('Object|Name')})
    panel_id = Column(Integer, ForeignKey('layout_panel.id'), info={'verbose': _('Panel')})
    component = Column(String(255), info={'verbose': _('Component')})
    options = Column(Text, info={'verbose': _('Options')})
    weight = Column(Integer, default=0, info={'verbose': _('Weight')})
    login_required = Column(Boolean, default=False)
    
    apps = relation('App', secondary=panel_block_apps)
    
    def __unicode__(self):
        return self.name
    
    def __repr__(self):
        return self.name


class HomepageBlock(Base):
    __tablename__ = 'layout_homepage_block'
    
    id = Column(Integer, primary_key=True)
    
    url = Column(Text, info={'verbose': _('Address')})
    caption = Column(String(30), info={'verbose': _('Caption')})
    description = Column(String(255), info={'verbose': _('Description')})
    icon = Column(String(255), info={'verbose': _('Icon')})
    
    component = Column(String(255), info={'verbose': _('Component')})
    options = Column(Text, info={'verbose': _('Options')})
    row = Column(Integer, info={'verbose': _('Row')})
    col = Column(Integer, info={'verbose': _('Column')})
    rowspan = Column(Integer, default=1)
    colspan = Column(Integer, default=1)
    
    is_disabled = Column(Boolean, default=False)


class Theme(Base):
    __tablename__ = 'layout_theme'
    
    id = Column(Integer, primary_key=True)
    name = Column(Text, info={'verbose': _('Object|Name')})
    
    def __unicode__(self):
        return self.name
